<template>
  <div class="image-editor">
    <Toolbar
      :can-undo="historyOps.canUndo"
      :can-redo="historyOps.canRedo"
      @upload-image="imageOps.uploadImage"
      @change-background="imageOps.changeBackground"
      @download="imageOps.downloadImage"
      @undo="historyOps.undo"
      @redo="historyOps.redo"
      @rotate-image="transformOps.rotateImage"
      @flip-image="transformOps.flipImage"
      @reset-transform="transformOps.resetTransform"
      @toggle-drawing="drawingOps.toggleDrawing"
      @set-drawing-mode="drawingOps.setDrawingMode"
      @set-drawing-color="drawingOps.setDrawingColor"
      @set-drawing-width="drawingOps.setDrawingWidth"
      @add-shape="drawingOps.addShape"
      @add-text="textOps.addText"
      @change-text-color="textOps.changeTextColor"
      @change-text-size="textOps.changeTextSize"
      @apply-filter="handleFilter"
      @set-blend-mode="blendOps.setBlendMode"
      @set-opacity="blendOps.setOpacity"
      @start-cropping="cropOps.startCropping"
      @apply-crop="cropOps.applyCrop"
      @cancel-crop="cropOps.cancelCrop"
      @start-selection="handleStartSelection"
      @clear-selection="selectionOps.clearSelection"
      @set-tolerance="value => selectionOps.tolerance = value"
    />
    
    <div class="editor-container">
      <!-- 图层面板 -->
      <div class="layers-panel" v-if="showLayers">
        <h3>图层</h3>
        <el-scrollbar height="calc(100vh - 200px)">
          <div
            v-for="layer in layerOps.layers"
            :key="layer.id"
            class="layer-item"
            :class="{ active: layer.id === activeLayerId }"
          >
            <el-checkbox
              v-model="layer.visible"
              @change="() => layerOps.toggleLayerVisibility(layer.id)"
            />
            <span class="layer-name">{{ layer.type }}</span>
            <div class="layer-actions">
              <el-button-group size="small">
                <el-button
                  :type="layer.locked ? 'primary' : ''"
                  @click="() => layerOps.toggleLayerLock(layer.id)"
                >
                  <el-icon><Lock /></el-icon>
                </el-button>
                <el-dropdown trigger="click" @command="(cmd) => handleLayerAction(cmd, layer.id)">
                  <el-button>
                    <el-icon><More /></el-icon>
                  </el-button>
                  <template #dropdown>
                    <el-dropdown-menu>
                      <el-dropdown-item command="duplicate">复制</el-dropdown-item>
                      <el-dropdown-item command="delete">删除</el-dropdown-item>
                      <el-dropdown-item divided command="top">置顶</el-dropdown-item>
                      <el-dropdown-item command="up">上移</el-dropdown-item>
                      <el-dropdown-item command="down">下移</el-dropdown-item>
                      <el-dropdown-item command="bottom">置底</el-dropdown-item>
                    </el-dropdown-menu>
                  </template>
                </el-dropdown>
              </el-button-group>
            </div>
          </div>
        </el-scrollbar>
      </div>

      <!-- 画布容器 -->
      <div class="canvas-container" ref="canvasContainer">
        <canvas ref="canvas"></canvas>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import { Lock, More } from '@element-plus/icons-vue'
import Toolbar from './editor/Toolbar.vue'
import { useCanvas } from '../composables/useCanvas'
import { useImageOperations } from '../composables/useImageOperations'
import { useTextOperations } from '../composables/useTextOperations'
import { useDrawing } from '../composables/useDrawing'
import { useFilters } from '../composables/useFilters'
import { useLayers } from '../composables/useLayers'
import { useHistory } from '../composables/useHistory'
import { useCropping } from '../composables/useCropping'
import { useTransform } from '../composables/useTransform'
import { useBlendModes } from '../composables/useBlendModes'
import { useSelection } from '../composables/useSelection'

// 初始化各个功能模块
const { canvas, canvasContainer, initCanvas, getCanvas } = useCanvas()
const imageOps = useImageOperations(getCanvas)
const textOps = useTextOperations(getCanvas)
const drawingOps = useDrawing(getCanvas)
const filterOps = useFilters(getCanvas)
const layerOps = useLayers(getCanvas)
const historyOps = useHistory(getCanvas)
const cropOps = useCropping(getCanvas)
const transformOps = useTransform(getCanvas)
const blendOps = useBlendModes(getCanvas)
const selectionOps = useSelection(getCanvas)

// 界面状态
const showLayers = ref(true)
const activeLayerId = ref(null)

// 处理滤镜
const handleFilter = (filterName) => {
  if (filterName === 'remove-filters') {
    filterOps.removeAllFilters()
  } else {
    filterOps.applyFilter(filterName)
  }
}

// 处理图层操作
const handleLayerAction = (action, layerId) => {
  switch (action) {
    case 'duplicate':
      layerOps.duplicateLayer(layerId)
      break
    case 'delete':
      layerOps.deleteLayer(layerId)
      break
    case 'top':
    case 'up':
    case 'down':
    case 'bottom':
      layerOps.reorderLayer(layerId, action)
      break
  }
}

// 处理选区操作
const handleStartSelection = (mode) => {
  selectionOps.clearSelection()
  switch (mode) {
    case 'rect':
      selectionOps.startRectSelection()
      break
    case 'lasso':
      selectionOps.startLassoSelection()
      break
    case 'magic-wand':
      const fabricCanvas = getCanvas()
      if (fabricCanvas) {
        fabricCanvas.on('mouse:down', (e) => {
          const pointer = fabricCanvas.getPointer(e.e)
          selectionOps.startMagicWandSelection(pointer.x, pointer.y)
        })
      }
      break
  }
}

// 初始化
onMounted(() => {
  initCanvas()
  layerOps.initLayerObserver()
  historyOps.initHistoryObserver()
})
</script>

<style scoped>
.image-editor {
  width: 100%;
  height: 100vh;
  display: flex;
  flex-direction: column;
  background-color: #f0f2f5;
}

.editor-container {
  flex: 1;
  display: flex;
  overflow: hidden;
}

.layers-panel {
  width: 250px;
  background: #fff;
  border-right: 1px solid #e4e7ed;
  padding: 16px;
}

.layers-panel h3 {
  margin: 0 0 16px;
  font-size: 16px;
  color: #303133;
}

.layer-item {
  display: flex;
  align-items: center;
  padding: 8px;
  border-radius: 4px;
  margin-bottom: 4px;
  transition: background-color 0.2s;
}

.layer-item:hover {
  background-color: #f5f7fa;
}

.layer-item.active {
  background-color: var(--el-color-primary-light-9);
}

.layer-name {
  flex: 1;
  margin: 0 8px;
  font-size: 14px;
  color: #606266;
}

.layer-actions {
  opacity: 0;
  transition: opacity 0.2s;
}

.layer-item:hover .layer-actions {
  opacity: 1;
}

.canvas-container {
  flex: 1;
  display: flex;
  justify-content: center;
  align-items: center;
  overflow: hidden;
  padding: 20px;
}

canvas {
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15);
}
</style> 